package com.wkbb.sports.controller;

import com.github.pagehelper.PageInfo;
import com.wkbb.common.base.BaseController;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.common.handler.JsonUtil;
import com.wkbb.sports.dto.TeachDocumentDto;
import com.wkbb.sports.dto.TeachFileDto;
import com.wkbb.sports.service.TeachFileService;
import com.wkbb.sports.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@Api(description = "教学资源管理")
@RequestMapping("/sports/{apiVersion}/teachFile")
@Slf4j
public class TeachFileController extends BaseController{
	
	@Autowired
	private TeachFileService teachFileService;
	
	
	@ApiOperation(value = "添加教学资源")
	@RequestMapping(value = "/addTeachFiles", method = RequestMethod.POST)
	public ResultDTO<?> addTeachFiles(
			@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
			@ApiParam(value = "教学资源", required = true) @RequestBody List<TeachFileDto> dtos){
		UserDto userDto = this.getUserByLogin();
		if(null == userDto) {
			return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
		}
		try{
			Date currDate = new Date();
			for (TeachFileDto dto:dtos){
				dto.setCreateUser(userDto.getName());
				dto.setCreateTime(currDate);
				if (dto.getTerminal() == null){
					dto.setTerminal(0);
				}
			}
			teachFileService.insertBatch(dtos);
		}catch(Throwable e) {
			logError(e);
			return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
		}
		return new ResultDTO<>();
	}

	@ApiOperation(value = "修改教学资源")
	@RequestMapping(value = "/updateTeachFile", method = RequestMethod.POST)
	public ResultDTO<?> updateTeachFile(
			@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
			@ApiParam(value = "教学资源", required = true) @RequestBody TeachFileDto dto){
		UserDto userDto = this.getUserByLogin();
		if(null == userDto) {
			return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
		}
		try{
			teachFileService.update(dto);
		}catch(Throwable e) {
			logError(e);
			return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
		}
		return new ResultDTO<>();
	}

	@ApiOperation(value = "查询教学资源列表")
	@RequestMapping(value = "/getTeachFileList", method = RequestMethod.GET)
	public ResultDTO<List<TeachFileDto>> getTeachFileList(
			@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
			@ApiParam(value = "ID列表", required = true) @RequestParam(name = "ids", required = true) String ids){
		UserDto userDto = this.getUserByLogin();
		if(null == userDto) {
			return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
		}
		Map<String,Object>  map  = new HashMap<>();
		map.put("ids", ids);
		try{
			List<TeachFileDto> dtos = teachFileService.getListByMapParams(map);
			return new ResultDTO<>(dtos);
		}catch(Throwable e) {
			logError(e);
			return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
		}
	}

	@ApiOperation(value = "删除教学资源")
	@RequestMapping(value = "/delTeachFile", method = RequestMethod.POST)
	public ResultDTO<?> addTeachFile(
			@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
			@ApiParam(value = "ID", required = false) @RequestParam(name = "id", required = false) Long id){
		UserDto userDto = this.getUserByLogin();
		if(null == userDto) {
			return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
		}
		try{
			teachFileService.deleteByPrimaryKey(id);
		}catch(Throwable e) {
			logError(e);
			return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
		}
		return new ResultDTO<>();
	}

	@ApiOperation(value = "分页查询教学资源")
	@RequestMapping(value = "/findTeachFileList", method = RequestMethod.GET)
	public ResultDTO<PageInfo<TeachFileDto>> findTeachFileList(
			@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
			@ApiParam(value = "学校id", required = false) @RequestParam(name = "orgId", required = false) Long orgId,
			@ApiParam(value = "项目id", required = false) @RequestParam(name = "projectId", required = false) String projectId,
			@ApiParam(value = "标签", required = false) @RequestParam(name = "tagId", required = false) String tagId,
			@ApiParam(value = "年级", required = false) @RequestParam(name = "gradeId", required = false) String gradeId,
			@ApiParam(value = "显示终端 1小程序", required = false) @RequestParam(name = "terminal", required = false) String terminal,
			@ApiParam(value = "分页数", defaultValue = "1") @RequestParam(name = "pageNo") int pageNo,
			@ApiParam(value = "分页大小", defaultValue = "10") @RequestParam(name = "pageSize") int pageSize){
		UserDto userDto = this.getUserByLogin();
		if(null == userDto) {
			return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
		}
		Map<String,Object>  map  = new HashMap<>();
		if (orgId != null && orgId != 1 ){
			map.put("orgId", orgId);
		}
		map.put("projectId", projectId);
		map.put("tagId", tagId);
		map.put("gradeId", gradeId);
		map.put("terminal", terminal);
		PageInfo<TeachFileDto> dtos = null;
		
		try {
			dtos = teachFileService.getListByPage(map,pageNo,pageSize);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResultDTO<>(dtos);
	}

	private void logError(Throwable e) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		e.printStackTrace(pw);
		pw.flush();
		log.error(sw.toString());
	}

}
